#include "soapH.h"
#include "netbook.nsmap"

typedef void (*HandlerFunc)(void *);

const char *server = "localhost:8888";

// 记得改成统一的风格 
int login(const char *server, HandlerFunc handler, char *mail, char *passwd, char **result);
int getMyNoteList(const char *server, HandlerFunc handler, char *uuid, uint32_t start, uint32_t total, uint32_t field, NoteArray **result);

void
print_string(void *data)
{
    if (data) {
	printf("result = %s\n",(char *)data);
    }
}

int
main(int argc, char **argv)
{
    double a, b;
    char *s = (char *)malloc(1024);
    char *retstr = NULL;
    char *session;
    if (argc < 4) {
	fprintf(stderr, "Usage: [add|sub|mul|div|pow] num num [server]\n");
	exit(0);
    }

    if (*argv[1] != 'u') {
	a = strtod(argv[2], NULL);
	b = strtod(argv[3], NULL);
    }

    if (argc >=5) {
	server = argv[4];
    }
    else {
	strcpy(s,argv[2]);
	strcat(s,argv[3]);
    }

    switch (*argv[1]) {
	case 'l':
		login(server, print_string, "test@test.com", "test123", &session);
	    break;
	default:
	    fprintf(stderr, "Unknown command\n");
	    exit(0);
    }

    return 0;
}


int
login(const char *server, HandlerFunc handler, char *mail, char *passwd, char **result)
{
    struct soap soap;
    soap_init(&soap);
    soap_set_namespaces(&soap, namespaces);

    // if result is null, alloc memery for res,or set res to result.
    char *res = NULL;
    if (result) {
	res = *result;
    }

    char *media = NULL;
    // call web service.
    soap_call_ns__login(&soap, server, "", mail, passwd, &media);

    if (soap.error) {
	soap_print_fault(&soap, stderr);
	exit(1);
    }
    if (media) {
	res = strdup(media);
    }
    if (handler) {
	handler((void *)res);
	getMyNoteList(server, NULL, res, 0, 10, 0, NULL);
    }

    soap_destroy(&soap);
    soap_end(&soap);
    soap_done(&soap);

    return 0;
}

int
getMyNoteList(const char *server, HandlerFunc handler, char *uuid, uint32_t start, uint32_t total, uint32_t field, NoteArray **result)
{
    struct soap soap;
    soap_init(&soap);
    soap_set_namespaces(&soap, namespaces);

    // if result is null, alloc memery for res,or set res to result.
    NoteArray *res = NULL;
    if (result) {
	res = *result;
    }
    else {
	//res = (user *)malloc(sizeof(user));
	//memset(res,0,sizeof(user));
    }

    NoteArray *media = NULL;
    // call web service.
    soap_call_ns__getMyNoteList(&soap, server, "", uuid, start, total, (1023), &media);

    if (soap.error) {
	soap_print_fault(&soap, stderr);
	exit(1);
    }

    if (media) {
	res = (NoteArray *)malloc(sizeof(NoteArray));
	memcpy(res, media, sizeof(NoteArray));
    }

    int i;
    if (!handler) {
	for (i = 0; i < media->__size; i++) {
	    fprintf(stderr, "%d\t", (media->__ptr)[i].id);
	    fprintf(stderr, "%s\n", (media->__ptr)[i].title);
	    fprintf(stderr, "%s\n", (media->__ptr)[i].content);
	    fprintf(stderr, "%s", (media->__ptr)[i].create_USCOREtime);
	    puts("");
	}
    }

    soap_destroy(&soap);
    soap_end(&soap);
    soap_done(&soap);


    return 0;
}
